home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / nrcmd.c < prev    next >
C/C++ Source or Header  |  1992-06-18  |  26KB  |  1,280 lines

  1. /* net/rom user command processing
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "config.h"
  9. #include "mbuf.h"
  10. #include "ax25.h"
  11. #include "timer.h"
  12. #include "iface.h"
  13. #include "lapb.h"
  14. #include "netrom.h"
  15. #include "nr4.h"
  16. #include "netuser.h"
  17. #include "tcp.h"
  18. #include "ftp.h"
  19. #include "telnet.h"
  20. #include "finger.h"
  21. #include "ax_mbx.h"
  22. #include "cmdparse.h"
  23. #include "session.h"
  24. #include <ctype.h>
  25. #ifdef    UNIX
  26. #undef    toupper
  27. #undef    tolower
  28. #include <memory.h>
  29. #include <string.h>
  30. #endif
  31.  
  32. #undef NRDEBUG
  33. static int donodetick();
  34. static int doobsotick();
  35. static donfdump();
  36. static void donrdump();
  37. long atol();
  38. extern struct session *current;
  39.  
  40. char *Nr4states[] = {
  41.     "Disconnected",
  42.     "Conn Pending",
  43.     "Connected",
  44.     "Disc Pending"
  45. } ;
  46.  
  47. char *Nr4reasons[] = {
  48.     "Normal",
  49.     "By Peer",
  50.     "Timeout",
  51.     "Reset",
  52.     "Refused"
  53. } ;
  54.  
  55. static int dointerface(), dobcnodes(), donodetimer(), donrroute(),
  56.            donrttl(), doobsotimer(), donodefilter(), donrverbose(),
  57.            donrconnect(), donrreset(), donrwindow(), donrirtt(),
  58.            donracktime(), donrqlimit(), donrchoketime(), donrretries(),
  59.            donrstatus(), donrkick() ;
  60.  
  61. static struct cmds nrcmds[] = {
  62.     "acktime",    donracktime,    0,    NULLCHAR,    NULLCHAR,
  63.     "bcnodes",    dobcnodes,    2,    "netrom bcnodes <interface>", NULLCHAR,
  64.     
  65. /* Put connect before choketime to make it the default expansion of 'c' */
  66.  
  67.     "connect",    donrconnect,2,    "netrom connect <node>",    NULLCHAR,
  68.     "choketime",    donrchoketime,    0,    NULLCHAR,    NULLCHAR,
  69.     "interface",    dointerface,    4,
  70.         "netrom interface <interface> <alias> <quality>",    NULLCHAR,
  71.     "irtt",            donrirtt,        0,    NULLCHAR,    NULLCHAR,
  72.     "kick",            donrkick,        2,    "netrom kick <&nrcb>",    NULLCHAR,
  73.     "nodefilter",    donodefilter,    0,    NULLCHAR,    NULLCHAR,
  74.     "nodetimer",    donodetimer,    0,    NULLCHAR,    NULLCHAR,
  75.     "obsotimer",    doobsotimer,    0,    NULLCHAR,    NULLCHAR,
  76.     "qlimit",    donrqlimit,    0,    NULLCHAR,    NULLCHAR,
  77.     "reset",    donrreset,    2,    "netrom reset <&nrcb>",    NULLCHAR,
  78.     "retries",    donrretries,0,    NULLCHAR,    NULLCHAR,
  79.     "route",    donrroute,    0,    NULLCHAR,    NULLCHAR,
  80.     "status",    donrstatus,    0,    NULLCHAR,    NULLCHAR,
  81.     "ttl",        donrttl,    0,    NULLCHAR,    NULLCHAR,
  82.     "verbose",    donrverbose,0,    NULLCHAR,    NULLCHAR,
  83.     "window",    donrwindow,    0,    NULLCHAR,    NULLCHAR,
  84.     NULLCHAR,    NULLFP,        0,
  85.         "netrom subcommands: acktime bcnodes connect choketime interface irtt kick\n                    nodetimer nodefilter obsotimer qlimit reset retries route\n                    status ttl verbose window",
  86.         NULLCHAR
  87. } ;
  88.  
  89. static struct timer nodetimer ;    /* timer for nodes broadcasts */
  90. static struct timer obsotimer ;    /* timer for aging routes */
  91.  
  92. /* Command multiplexer */
  93. donetrom(argc,argv)
  94. int argc ;
  95. char *argv[] ;
  96. {
  97.     return subcmd(nrcmds,argc,argv) ;
  98. }
  99.  
  100. static int dorouteadd(), doroutedrop(), doroutedump(), dorouteinfo() ;
  101.  
  102. static struct cmds routecmds[] = {
  103.     "add",    dorouteadd,    6,
  104.         "netrom route add <alias> <destination> <interface> <quality> <neighbor>",
  105.         "add failed",
  106.     "drop",    doroutedrop, 4,
  107.         "netrom route drop <destination> <neighbor> <interface>",
  108.         "drop failed",
  109.     "info", dorouteinfo, 2,
  110.         "netrom route info <destination>", NULLCHAR,
  111.     NULLCHAR,    NULLFP,    0,
  112.         "netrom route subcommands: add drop info",
  113.         NULLCHAR
  114. } ;
  115.  
  116. /* Route command multiplexer */
  117. static
  118. donrroute(argc, argv)
  119. int argc ;
  120. char *argv[] ;
  121. {
  122.     if (argc < 2) {
  123.         doroutedump() ;
  124.         return 0 ;
  125.     }
  126.     return subcmd(routecmds,argc,argv) ;
  127. }
  128.  
  129. /* Dump a list of known routes */
  130. static
  131. doroutedump()
  132. {
  133.     register struct nrroute_tab *rp ;
  134.     register int i, column ;
  135.     char buf[32] ;
  136.     char *cp ;
  137.     int count = 0;
  138.     
  139.     column = 1 ;
  140.     
  141.     for (i = 0 ; i < NRNUMCHAINS ; i++)
  142.         for (rp = nrroute_tab[i] ; rp != NULLNRRTAB ; rp = rp->next) {
  143.             strcpy(buf,rp->alias) ;
  144.             /* remove trailing spaces */
  145.             if ((cp = index(buf,' ')) == NULLCHAR)
  146.                 cp = &buf[strlen(buf)] ;
  147.             if (cp != buf)        /* don't include colon for null alias */
  148.                 *cp++ = ':' ;
  149.             pax25(cp,&rp->call) ;
  150.             printf("%-16s  ",buf) ;
  151.             count++;
  152.             if (column++ == 4) {
  153.                 printf("\n") ;
  154.                 column = 1 ;
  155.             }
  156.         }
  157.  
  158.     if (column != 1)
  159.         printf("\n") ;
  160.     printf("Total station count = %d\n",count);
  161.         
  162.     return 0 ;
  163. }
  164.  
  165. /* print detailed information on an individual route */
  166. /*ARGSUSED*/
  167. static int
  168. dorouteinfo(argc,argv)
  169. int argc ;
  170. char *argv[] ;
  171. {
  172.     register struct nrroute_tab *rp ;
  173.     register struct nr_bind *bp ;
  174.     register struct nrnbr_tab *np ;
  175.     struct ax25_addr dest ;
  176.     char neighbor[60] ;
  177.  
  178.     if (setcall(&dest,argv[1]) == -1) {
  179.         printf ("bad destination name\n") ;
  180.         return -1 ;
  181.     }
  182.         
  183.     if ((rp = find_nrroute(&dest)) == NULLNRRTAB) {
  184.         printf("no such route\n") ;
  185.         return -1 ;
  186.     }
  187.  
  188.     for (bp = rp->routes ; bp != NULLNRBIND ; bp = bp->next) {
  189.         np = bp->via ;
  190.         psax25(neighbor,np->call) ;
  191.         printf("%1s %3d  %3d  %-8s  %s\n",
  192.                 (bp->flags & NRB_PERMANENT ? "P" :
  193.                  bp->flags & NRB_RECORDED ? "R" : " "),
  194.                 bp->quality,bp->obsocnt,
  195.                 nrifaces[np->interface].interface->name,
  196.                 neighbor) ;
  197.     }
  198.     return 0 ;
  199. }
  200.         
  201. /* convert a null-terminated alias name to a blank-filled, upcased */
  202. /* version.  Return -1 on failure. */
  203. static int
  204. putalias(to,from,complain)
  205. register char *to, *from ;
  206. int complain ;
  207. {
  208.     int len, i ;
  209.     
  210.     if ((len = strlen(from)) > ALEN) {
  211.         if (complain)
  212.             printf ("alias too long - six characters max\n") ;
  213.         return -1 ;
  214.     }
  215.     
  216.     for (i = 0 ; i < ALEN ; i++) {
  217.         if (i < len) {
  218.             if (islower(*from))
  219.                 *to++ = toupper(*from++) ;
  220.             else
  221.                 *to++ = *from++ ;
  222.         }
  223.         else
  224.             *to++ = ' ' ;
  225.     }
  226.             
  227.     *to = '\0' ;
  228.     return 0 ;
  229. }
  230.  
  231. /* Add a route */
  232. static int
  233. dorouteadd(argc, argv)
  234. int argc ;
  235. char *argv[] ;
  236. {
  237.     char alias[7] ;
  238.     struct ax25_addr dest ;
  239.     unsigned quality ;
  240.     char neighbor[AXALEN * 3] ;
  241.     register int i ;
  242.     int naddr ;
  243.  
  244.     /* format alias (putalias prints error message if necessary) */
  245.     if (putalias(alias,argv[1],1) == -1)
  246.         return -1 ;
  247.  
  248.     /* format destination callsign */
  249.     if (setcall(&dest,argv[2]) == -1) {
  250.         printf("bad destination callsign\n") ;
  251.         return -1 ;
  252.     }
  253.  
  254.     /* find interface */
  255.     for (i = 0 ; i < nr_numiface ; i++)
  256.         if (!strcmp(nrifaces[i].interface->name,argv[3]))
  257.             break ;
  258.     if (i == nr_numiface) {
  259.         printf("Interface \"%s\" not found\n",argv[3]) ;
  260.         return -1 ;
  261.     }
  262.     
  263.     /* get and check quality value */
  264.     if ((quality = atoi(argv[4])) > 255) {
  265.         printf("maximum route quality is 255\n") ;
  266.         return -1 ;
  267.     }
  268.  
  269.     /* make sure no more than 2 digis */
  270.     naddr = argc - 5 ;
  271.     if (naddr > 3) {
  272.         printf("no more than 2 digipeaters for a net/rom neighbor\n") ;
  273.         return -1 ;
  274.     }
  275.     
  276.     /* format neighbor address string */
  277.     setpath(neighbor,&argv[5],naddr) ;
  278.  
  279.     return nr_routeadd(alias,&dest,(unsigned)i,quality,neighbor,1,0) ;
  280. }
  281.  
  282.  
  283. /* drop a route */
  284. /*ARGSUSED*/
  285. static
  286. doroutedrop(argc,argv)
  287. int argc ;
  288. char *argv[] ;
  289. {
  290.     struct ax25_addr dest, neighbor ;
  291.     register int i ;
  292.  
  293.     /* format destination and neighbor callsigns */
  294.     if (setcall(&dest,argv[1]) == -1) {
  295.         printf("bad destination callsign\n") ;
  296.         return -1 ;
  297.     }
  298.     if (setcall(&neighbor,argv[2]) == -1) {
  299.         printf("bad neighbor callsign\n") ;
  300.         return -1 ;
  301.     }
  302.  
  303.     /* find interface */
  304.     for (i = 0 ; i < nr_numiface ; i++)
  305.         if (!strcmp(nrifaces[i].interface->name,argv[3]))
  306.             break ;
  307.     if (i == nr_numiface) {
  308.         printf("Interface \"%s\" not found\n",argv[3]) ;
  309.         return -1 ;
  310.     }
  311.  
  312.     return nr_routedrop(&dest,&neighbor,(unsigned)i) ;
  313. }
  314.     
  315.     
  316. /* make an interface available to net/rom */
  317. /*ARGSUSED*/
  318. static int
  319. dointerface(argc,argv)
  320. int argc ;
  321. char *argv[] ;
  322. {
  323.     int i ;
  324.     register struct interface *ifp ;
  325.     extern struct interface *ifaces ;
  326.  
  327.     if (nr_interface == NULLIF) {
  328.         printf("Attach netrom interface first\n") ;
  329.         return 1 ;
  330.     }
  331.     
  332.     if (nr_numiface >= NRNUMIFACE) {
  333.         printf("Only %d net/rom interfaces available\n",NRNUMIFACE) ;
  334.         return 1 ;
  335.     }
  336.     
  337.     for(ifp=ifaces;ifp != NULLIF;ifp = ifp->next){
  338.         if(strcmp(argv[1],ifp->name) == 0)
  339.             break;
  340.     }
  341.     if(ifp == NULLIF){
  342.         printf("Interface \"%s\" unknown\n",argv[1]);
  343.         return 1;
  344.     }
  345.     for (i = 0 ; i < nr_numiface ; i++)
  346.         if (nrifaces[i].interface == ifp) {
  347.             printf("Interface \"%s\" is already registered\n",argv[1]) ;
  348.             return 1 ;
  349.         }
  350.         
  351.     nrifaces[nr_numiface].interface = ifp ;
  352.  
  353.     if (putalias(nrifaces[nr_numiface].alias,argv[2],1) == -1)
  354.         return 1 ;
  355.         
  356.     if ((nrifaces[nr_numiface].quality = atoi(argv[3])) > 255) {
  357.         printf("Q